﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Work02_P1.Pojo;
using Work02_P1.Mapper;

namespace Work02_P1
{
    public partial class AddContact : Form
    {
        public AddContact()
        {
            InitializeComponent();
        }

        Main main = null;
        String userId = null;
        String preName="";
        String prePhone="";
        int tip=0;
        public AddContact(Main main,String userId)
        {
            InitializeComponent();
            this.main = main;
            this.userId = userId;
            preName = "";
            prePhone = "";
            
        }

        public AddContact(Main main, String userId,String name,String phone)
        {
            InitializeComponent();
            this.main = main;
            this.userId = userId;
            textBox1.Text = name;
            textBox2.Text = phone;
            preName = name;
            prePhone = phone;
            tip = 1;
        }

        private void AddContact_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String name = this.textBox1.Text;
            String phone = this.textBox2.Text;
            if (tip == 1)
            {
                if (phone.Equals(prePhone))
                {
                    MessageBox.Show("请填写新的电话！");
                    return;
                }
                if (!preName.Equals(name))
                {
                    MessageBox.Show("非法操作，本次操作被锁定！");
                    backToMain();
                    return;
                }
            }


            if (name.Equals("") || phone.Equals("")||phone.Length!=11)

            {
                textBox1.Text = "";
                textBox2.Text = "";
                MessageBox.Show("信息填写不规范！");
                return;
            }

            foreach(char co in phone)
            {
                if (co < '0' || co > '9')
                {
                    MessageBox.Show("信息填写不规范！");
                    textBox2.Text = "";
                }
            }

            Contact c = new Contact(userId,name,phone);
            if (UserMapper.insertContact(c))
            {
                MessageBox.Show("操作成功！");
                if (tip == 1)
                {
                    
                    UserMapper.deleteContact(userId, preName, prePhone);
                    
                }
               
                backToMain();
            }
            else
            {
                MessageBox.Show("操作失败！");
            }


        }
        private void backToMain()
        {
            this.Close();
            main.Show();
            main.Loader();
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            backToMain();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
